package com.fnst.facedetect.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fnst.facedetect.dao.CommonDao;
import com.fnst.facedetect.model.CompareData;
import com.fnst.facedetect.model.DetectedInfo;
import com.fnst.facedetect.model.PhotoData;
import com.fnst.facedetect.service.ComparePhotoService;
import com.fnst.facedetect.service.GetPhotoService;
import com.fnst.facedetect.util.Utils;

/**
 * Servlet implementation class FaceDetectServlet
 */
public class FaceDetectServlet extends AbstractDetectServlet {
	private static final long serialVersionUID = 1L;
       
	protected Logger log = LoggerFactory.getLogger(getClass());
	private CommonDao commonDao;
	private GetPhotoService gps = new GetPhotoService();
	private ComparePhotoService cps = new ComparePhotoService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FaceDetectServlet() {
        super();
        try {
			commonDao = CommonDao.getInstance();
		} catch (Exception e) {
			log.error("RecordShare Servlet Init error", e);
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//ComparePhotoService
		//photo_info
		//identify_card_id
		//{"result":"85","errorMsg":"ok"}

		String cardId = request.getParameter("identify_card_id");
		String photoInfo = request.getParameter("photo_info");
		
		log.info("cardId: " + cardId);
		log.debug("photoInfo: " + photoInfo);
		
		DetectedInfo detectedInfo = new DetectedInfo();
		detectedInfo.setIdentityCardId(cardId);
		detectedInfo.setUploadTime(new Date());
		//开始检测
		detectedInfo.setCompareResult("-2");
		commonDao.addDetectedInfo(detectedInfo);
		savePicture(photoInfo, getUploadPath() + cardId + "_" + detectedInfo.getId() + ".jpg");
		
		if(cardId != null){
			cardId = cardId.toUpperCase();
		}else{
			Map m = new HashMap();
			m.put("result", "-3");
			m.put("errorMsg", "identify_card_id null");
			detectedInfo.setCompareResult("-3");
			detectedInfo.setErrorMsg("identify_card_id null");
			commonDao.updateDetectInfo(detectedInfo);
			Utils.responseTextFromMap(response, m);
			return;
		}
		
		if(photoInfo == null){
			Map m = new HashMap();
			m.put("result", "-4");
			m.put("errorMsg", "photo_info null");
			detectedInfo.setCompareResult("-4");
			detectedInfo.setErrorMsg("photo_info null");
			commonDao.updateDetectInfo(detectedInfo);
			Utils.responseTextFromMap(response, m);
			return;
		}
		
		if(!Utils.isIdCorrect(cardId)){
			log.error("非法身份证号:"+cardId);
			Map m = new HashMap();
			m.put("result", "-5");
			m.put("errorMsg", "非法身份证号");
			detectedInfo.setCompareResult("-5");
			detectedInfo.setErrorMsg("非法身份证号");
			commonDao.updateDetectInfo(detectedInfo);
			Utils.responseTextFromMap(response, m);
			return;
		}
		
		//获取证件照片
		PhotoData photoData = gps.execute(cardId);
		
		String pic2A = photoData.getPhoto1DataStr();		
		String pic2B = photoData.getPhoto2DataStr();
		
		savePicture(pic2A, getSavePath1() + cardId + "_" + detectedInfo.getId() + "_A.jpg");
		savePicture(pic2B, getSavePath2() + cardId + "_" + detectedInfo.getId() + "_B.jpg");
		
		if (pic2A == null && pic2B == null) {
			log.error("没有证件照片:"+cardId);
			Map m = new HashMap();
			m.put("result", "-6");
			m.put("errorMsg", "没有证件照片");
			detectedInfo.setCompareResult("-6");
			detectedInfo.setErrorMsg("没有证件照片");
			commonDao.updateDetectInfo(detectedInfo);
			Utils.responseTextFromMap(response, m);
			return;
		}
		
		CompareData compareData = new CompareData();
		compareData.setCardId(cardId);
		compareData.setComparePhoto(photoInfo);
		compareData.setIdPhoto1(photoData.getPhoto1DataStr());
		compareData.setIdPhoto2(photoData.getPhoto2DataStr());
		
		//对比照片
		cps.execute(compareData);
		
		detectedInfo.setCompareResult(compareData.getResult());
		detectedInfo.setErrorMsg(compareData.getErrorMsg());
		commonDao.updateDetectInfo(detectedInfo);
		
		Map resultMap = new HashMap();
		resultMap.put("result", compareData.getResult());
		resultMap.put("errorMsg", compareData.getErrorMsg());
		Utils.responseTextFromMap(response, resultMap);
	}

}
